package com.example.demo.service;

import com.example.demo.entity.Customer;
import com.example.demo.service.impl.CustomerServiceAJpaImpl;
import com.example.demo.service.impl.CustomerServiceAMybatisImpl;
import com.example.demo.service.impl.CustomerServiceBJpaImpl;
import com.example.demo.service.impl.CustomerServiceBMybatisImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @auther yaoyouwei
 * @create 2020-03-29 9:44
 */
@Service
public class MainService {
    @Resource(name = "customerServiceAJpaImpl")
    CustomerServiceAJpaImpl serviceA;
    @Resource(name = "customerServiceBJpaImpl")
    CustomerServiceBJpaImpl serviceB;

    @Resource(name = "customerServiceAMybatisImpl")
    CustomerServiceAMybatisImpl serviceA1;
    @Resource(name = "customerServiceBMybatisImpl")
    CustomerServiceBMybatisImpl serviceB1;

    @Transactional
    public boolean executeJpa() {
        Customer customerA = Customer.builder().firstName("yao").lastName("A").build();
        Customer customerB = Customer.builder().firstName("yao").lastName("B").build();
        serviceA.addRequired(customerA);

        try {
            serviceB.addNested(customerB);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Transactional
    public boolean executeMybatis() {
        Customer customerA = Customer.builder().firstName("yao").lastName("A").build();
        Customer customerB = Customer.builder().firstName("yao").lastName("B").build();
        serviceA1.addRequired(customerA);
//        serviceA1.addWithoutTransactional(customerA);
        try {
            // serviceB1.addWithoutTransactionalExc(customerB);
//            serviceB1.addRequiredExc(customerB);
            serviceB1.addRequiredNewExc(customerB);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /* @Transactional
    public boolean executeMybatis() {
        Customer customerA = Customer.builder().firstName("yao").lastName("A").build();
        Customer customerB = Customer.builder().firstName("yao").lastName("B").build();
        serviceA1.addWithoutTransactional(customerA);
        serviceA1.throwExc();
        return true;
    }*/
}
